package com.wits.qso.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.format.annotation.DateTimeFormat;

import com.wits.platform.entity.BaseEntity;

/**
 * 学生缴交作业实体，缴交流程承载对象
 */
@Entity
@Table(name = "QSO_HOMEWORK")
public class Homework extends BaseEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2374300135914679114L;
	/**
	 * 主键
	 */
	private String id = null;

	/**
	 * 布置作业ID
	 */

	private HomeworkH homeworkH;

	private Student student;

	private Teacher teacher;

	private Course course;

	/**
	 * 小组Id
	 */
	private String groupId;
	/**
	 * 作业名称
	 */
	private String homeworkName;

	/**
	 * 作业描述
	 */
	private String description = null;
	/**
	 * 缴交日期
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date handInDate;
	/**
	 * 批改日期
	 */
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date correctDate;

	/**
	 * 作业评分
	 */
	private int hwkGrade;

	/**
	 * 作业批语
	 */
	private String hwkComment;
	/**
	 * 作业状态
	 */
	private String hwkStatus;
	
	/**
	 * 附件列表
	 */
	@Transient
	private List<HWKAttachment> hwkAttachmentList=new ArrayList<HWKAttachment>();

	public Homework() {
		super();
	}

	@Id
	@GenericGenerator(name = "idGen", strategy = "com.wits.platform.common.id.generator.IDGenerator", parameters = {
			@Parameter(name = "segment_value", value = "QSO_HWK_ID"),
			@Parameter(name = "table_name", value = "wits_id_generator"),
			@Parameter(name = "value_column_name", value = "gen_value"),
			@Parameter(name = "segment_column_name", value = "gen_name"),
			@Parameter(name = "segment_value_length", value = "10"),
			@Parameter(name = "sq_format", value = "%08d"),
			@Parameter(name = "sq_prefix", value = "WK") })
	@GeneratedValue(generator = "idGen")
	@Column(name = "hwk_id", nullable = false, insertable = true, updatable = false, length = 10)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Column(name = "hwk_title", length = 100)
	public String getHomeworkName() {
		return homeworkName;
	}

	public void setHomeworkName(String homeworkName) {
		this.homeworkName = homeworkName;
	}

	@Column(name = "hwk_desc", length = 500)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "group_id", length = 10)
	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_handin")
	public Date getHandInDate() {
		return handInDate;
	}

	public void setHandInDate(Date handInDate) {
		this.handInDate = handInDate;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date_correct")
	public Date getCorrectDate() {
		return correctDate;
	}

	public void setCorrectDate(Date correctDate) {
		this.correctDate = correctDate;
	}

	@Column(name = "hwk_grade")
	public int getHwkGrade() {
		return hwkGrade;
	}

	public void setHwkGrade(int hwkGrade) {
		this.hwkGrade = hwkGrade;
	}

	@Column(name = "hwk_cmmt")
	public String getHwkComment() {
		return hwkComment;
	}

	public void setHwkComment(String hwkComment) {
		this.hwkComment = hwkComment;
	}

	@Column(name = "hwk_status")
	public String getHwkStatus() {
		return hwkStatus;
	}

	public void setHwkStatus(String hwkStatus) {
		this.hwkStatus = hwkStatus;
	}

	@ManyToOne
	@JoinColumn(name = "hwkh_id", updatable = true)
	public HomeworkH getHomeworkH() {
		return homeworkH;
	}

	public void setHomeworkH(HomeworkH homeworkH) {
		this.homeworkH = homeworkH;
	}

	@ManyToOne
	@JoinColumn(name = "course_id", updatable = true)
	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	@ManyToOne
	@JoinColumn(name = "stu_id", updatable = true)
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	@ManyToOne
	@JoinColumn(name = "ter_id", updatable = true)
	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
	@OneToMany(targetEntity=HWKAttachment.class, mappedBy="homework")
	public List<HWKAttachment> getHwkAttachmentList() {
		return hwkAttachmentList;
	}

	public void setHwkAttachmentList(List<HWKAttachment> hwkAttachmentList) {
		this.hwkAttachmentList = hwkAttachmentList;
	}

	public String display() {
		// TODO Auto-generated method stub
		return null;
	}

//	public String toString() {
//		return JSONObject.fromObject(this, Homework.getBaseJsonConfig())
//				.toString();
//	}
	
	public String toString(){
		StringBuffer buf=new StringBuffer();
		buf.append("{ ");
		buf.append("\"id\":\"").append(this.id);
		buf.append("\",\"teaName\":\"").append(this.teacher.getTerName());
		buf.append("\",\"hwkhId\":\"").append(this.homeworkH.getId());
		buf.append("\",\"hwkhName\":\"").append(this.homeworkH.getTitle());
		buf.append("\",\"groupId\":\"").append(this.groupId);
		buf.append("\",\"homeworkName\":\"").append(this.homeworkName);
		buf.append("\",\"description\":\"").append(this.description);
		buf.append("\",\"handInDate\":\"").append(this.handInDate);
		buf.append("\",\"correctDate\":\"").append(this.correctDate);
		buf.append("\",\"hwkGrade\":\"").append(this.hwkGrade);
		buf.append("\",\"hwkComment\":\"").append(this.hwkComment);
		buf.append("\",\"hwkStatus\":\"").append(this.hwkStatus);
		buf.append("\",\"stuName\":\"").append(this.student.getStuName());
		buf.append("\",\"courseName\":\"").append(this.course.getTitle());
		buf.append("\" }");
		return buf.toString();
	}
}
